package org.mics.organization.controller;

import java.util.List;

import javax.annotation.Resource;

import org.mics.core.page.PageQuery;
import org.mics.core.response.OneDataResponse;
import org.mics.core.response.PageDataResponse;
import org.mics.core.response.SimpleResponse;
import org.mics.jpa.helper.TreeUtils;
import org.mics.lang.bean.BeanConvertUtil;
import org.mics.organization.entity.CompanyDO;
import org.mics.organization.request.CompanyRequest;
import org.mics.organization.service.CompanyService;
import org.mics.organization.vo.CompanyVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 公司
 * @author mics
 * @date 2023-9-22
 * @version  1.0
 */
@RestController
@RequestMapping("/company")
@Tag(name = "公司")
public class CompanyController {
	@Resource
	private  CompanyService companyService;
	
	@Operation(summary =  "增加/修改公司")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
	public SimpleResponse save(@Validated @RequestBody CompanyRequest companyRequest) {
		CompanyDO companyDO = BeanConvertUtil.convert(companyRequest, CompanyDO.class);
		String id =   companyService.add(companyDO);
		return new OneDataResponse<String>(id);
	}
	
	@Operation(summary =  "删除公司")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
	public SimpleResponse delete(@PathVariable("id") String id) {
		companyService.delete(id);
        return new SimpleResponse();
	}
	
	@Operation(summary =  "公司分页列表")
    @RequestMapping(value = "/listByPage",method = RequestMethod.POST)
	public PageDataResponse<CompanyVO> listByPage(@RequestBody PageQuery pageQuery) {
		PageDataResponse<CompanyVO> pageDataResponse =   companyService.listByPage(pageQuery);
		return pageDataResponse;
	}
	
	@Operation(summary =  "公司树")
    @RequestMapping(value = "/tree",method = RequestMethod.GET)
	public OneDataResponse<CompanyVO> tree() {
		List<CompanyDO> companyDOs =  companyService.list();
		List<CompanyVO> companyVOs = BeanConvertUtil.convertList(companyDOs, CompanyVO.class);
		return new OneDataResponse<CompanyVO>(TreeUtils.convertOneTree(companyVOs));
	}
	
}
